//
//  LCDetailsViewController.m
//  EnterpriseCloud
//
//  Created by 东亨 on 2017/7/10.
//  Copyright © 2017年 lichao. All rights reserved.
//

#import "LCDetailsViewController.h"
#import "CountHeight.h"
#import "LCWebTableViewCell.h"
#import "MessageDetailModel.h"
static NSString *cellidentifier2 = @"LCWebTableViewCell";
@interface LCDetailsViewController ()<UITableViewDelegate,UITableViewDataSource,UIWebViewDelegate>
@property (weak, nonatomic) IBOutlet UITableView *tableView;
@property (strong, nonatomic) IBOutlet UIView *headView;
@property (weak, nonatomic) IBOutlet UILabel *detailTitle;
@property (weak, nonatomic) IBOutlet UILabel *typeLbl;
@property (weak, nonatomic) IBOutlet UILabel *timeLbl;
@property (weak, nonatomic) IBOutlet UILabel *tagNameLbl;
@property (copy,nonatomic)NSMutableDictionary  *dataDic;
@property (strong,nonatomic)MessageDetailModel *messageModel;

@end

@implementation LCDetailsViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    _dataDic = [[NSMutableDictionary alloc]initWithCapacity:0];
    [self loadCustomeView];
    [self loadData];
}
- (void)loadCustomeView
{
    _tableView.delegate = self;
    _tableView.dataSource = self;
    _tableView.scrollEnabled = NO;
    _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    _tableView.tableHeaderView =_headView;
    [_tableView registerNib:[UINib nibWithNibName:@"LCWebTableViewCell" bundle:nil] forCellReuseIdentifier:cellidentifier2];
}
- (void)loadData
{
    [_dataDic setObject:self.info_id forKey:@"info_id"];
    [MDataProvider messageDetailBodyDic:_dataDic sucess:^(NSMutableDictionary *resultDic) {
        _messageModel = resultDic[@"info"];
        _detailTitle.text = _messageModel.title;
        _timeLbl.text = _messageModel.published_date;
        _typeLbl.text = _messageModel.column_name;
        _detailTitle.text = _messageModel.summary;
         [self setBarTitle:_messageModel.column_name];
        NSArray  *listArr = [NSArray arrayWithArray:resultDic[@"tagList"]];
        NSString *listStr = @"";
        for (TagListModel *list in listArr)
        {
            NSString *str1 = [NSString stringWithFormat:@"%@/",list.tag_name];
            listStr = [listStr stringByAppendingString:str1];
        }
        if (listStr.length>0)
        {
             _tagNameLbl.text =  [listStr substringToIndex:listStr.length-1];
        }
        else
        {
            _tagNameLbl.text= listStr;
        }
        [_tableView reloadData];
        
    } faile:^(NSString *str) {
        [SVProgressHUD showErrorWithStatus:str];
    }];
}
#pragma mark -UITableViewDelegate
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 1;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    LCWebTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellidentifier2];
    [cell.webView loadHTMLString:_messageModel.content baseURL:nil];
    cell.webView.delegate = self;
    return cell;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return SCREEN_HEIGHT-180;
}
- (void)webViewDidFinishLoad:(UIWebView *)webView
{
    NSString *script = [NSString stringWithFormat:
                        @"var script = document.createElement('script');"
                        "script.type = 'text/javascript';"
                        "script.text = \"function ResizeImages() { "
                        "var img;"
                        "var maxwidth=%f;"
                        "for(i=0;i <document.images.length;i++){"
                        "img = document.images[i];"
                        "if(img.width > maxwidth){"
                        "img.width = maxwidth;"
                        "}"
                        "}"
                        "}\";"
                        "document.getElementsByTagName('head')[0].appendChild(script);",SCREEN_WIDTH - 36];
    [webView stringByEvaluatingJavaScriptFromString: script];
    [webView stringByEvaluatingJavaScriptFromString:@"ResizeImages();"];
}
@end
